$(function(){
    $("#productInfo_datagrid").datagrid({
        url:"/storages/list?state=2",
        fitColumns: true,
        fit: true,
        rownumbers: true,
        striped: true,
        singleSelect:true,
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'id', title: '编号', hidden: true, width: 100},
            {field: 'numbers', title: '保险单号', width: 100, align: 'right'},
            {field: 'clients', title: '投保人', width: 100 ,formatter: function (value, row, index) {
                return value? value.clientname:"";
            }},
            {field: 'put', title: '申请日期', width: 100},
            {field: 'durations', title: '保险时长', width: 100},
            {field: 'employees', title: '业务员', width: 100 ,formatter: function (value, row, index) {
                return value?"<p>"+value.username+"</p>" : "<p>有bug</p>";
            }},
            {field: 'limits', title: '投保总额度', width: 100, align: 'right'}
        ]],
        pagination: true,
        toolbar: "#tb",

    });
    //初始化对话框
    $("#my_dialog").dialog({
        width: 400,
        height: 550,
        closed: true
    })
    //初始化对话框-end
})

//投保人详情
function changeSate(){
    var row = $("#productInfo_datagrid").datagrid("getSelected");
    if(!row){
        $.messager.alert('温馨提示','亲,请先选中你要查的数据');
        return;
    }

    $("#my_dialog").dialog("open");
    $("#my_dialog").dialog("setTitle","查看投保人明细");
    //数据回显:同名原则
    //角色回显
    $.get("/client/queryClientIdsStoraId?stoId=" + row.clients.id,function(data){
        $("#editForm").form("load",data);
    })

}

//提审
function add(){
    var row = $("#productInfo_datagrid").datagrid("getSelected");
    if(!row){
        $.messager.alert('温馨提示','亲,请先选中你要查的数据');
        return;
    }
    $.get("/storages/storagesIdsStoraId?state=5&pay=0&id=" + row.id,function(data){
        //重载
        $("#productInfo_datagrid").datagrid("reload");
    })

}
//退回修改
function update(){
    var row = $("#productInfo_datagrid").datagrid("getSelected");
    if(!row){
        $.messager.alert('温馨提示','亲,请先选中你要查的数据');
        return;
    }
    $.get("/storages/storagesIdsStoraId?state=3&pay=0&id=" + row.id,function(data){
        //重载
        $("#productInfo_datagrid").datagrid("reload");
    })
}
//刷新
function refresh(){
    //重载
    $("#productInfo_datagrid").datagrid("reload");
}
//高级查询
function querys(){
    var kw = $("#keyword").textbox("getValue");
    $("#productInfo_datagrid").datagrid("load",{
        keyword:kw
    })
}

//拒保
function remove(){
    var row = $("#productInfo_datagrid").datagrid("getSelected");
    if(!row){
        $.messager.alert('温馨提示',"请选择要删除的行");
    }else{
        $.get("/storages/delete",{id:row.id},function(data){
            if(!data.success){
                $.messager.alert('温馨提示',data.msg);
            }else{
                $("#productInfo_datagrid").datagrid("reload");
            }
        })
    }

}

//保单详情
$(function () {
    $("#car_dialog").dialog({
        width: '650',
        height: '400',
        closable: true,
        closed: true,

    });
})
function queryCar(){
    $("#car_dialogplus").dialog("open");
    $("#car_dialogplus").dialog("setTitle","车辆信息");
    $("#car_datagrid").datagrid({
        fit:true,
        url:"/car/list",
        fitColumns:true,
        striped:true,
        pagination:true,
        singleSelect:true,
        rownumbers:true,
        onDblClickRow: function(index,row){
            row["car.displacement"] = row.displacement;
            row["car.carname"] = row.carname;
            row["car.cartype"] = row.cartype;
            row["car.marketval"] = row.marketval;
            row["car.sort"] = row.sort;
            row["car.carsizeId"] = row.carsizeId;
            row["car.id"] = row.id;

            $("#car_Form").form("load",row);
            $("#car_dialogplus").dialog("close");
        },
        columns:[[
            {field:'id',title:'编号', hidden:true},
            {field:'checkbox',checkbox:true,tilte:'选中',width:100},
            {field:'carname',title:'车俩品牌',align:'center',width:100},
            {field:'cartype',title:'车辆型号',align:'center',width:100},
            {field:'displacement',title:'排气量/L',align:'center',width:100},
            {field:'marketval',title:'排气量/L',align:'center',width:100},
            {field:'carsizeId',title:'分类',align:'center',width:100,formatter:function(value){
                if(value==1){
                    return "小型"
                }else if(value==2){
                    return "中型"
                }else if(value==3){
                    return "大型"
                }
            }},
            {field:'sort',title:'车载大小',align:'center',width:100,formatter:function(value){
                if(value){
                    return "<p style='color: #00bbee'>载人</p>";
                }else{
                    return "<p style='color: #e68900'>载物</p>";
                }
            }},
        ]],})

}
$(function(){
    $("#car_dialogplus").dialog({
        width:600,
        height:400,
        closable:true,
        closed:true

    })
})
function safes(){
    $("#car_dialogplus").dialog("open");
    $("#car_dialogplus").dialog("setTitle","产品信息");
    var row = $("#institution").val();
    $("#car_datagrid").datagrid({
        fit:true,
        url:"/productInfo/queryProductForaiId?id="+row,
        fitColumns:true,
        striped:true,
        pagination:true,
        singleSelect:true,
        rownumbers:true,
        onDblClickRow: function(index,row) {
            row["product_id"] = row.id;
            $("#info_form").form("load", row);
            $("#car_dialogplus").dialog("close");
            $("#in_datagrid").datagrid('appendRow',row);
        },
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'id', title: '编号', hidden: true, width: 100},
            {field: 'sn', title: '保险编码', width: 100, align: 'right'},
            {field: 'name', title: '产品名称', width: 100},
            {
                field: 'agency', title: '所属机构', width: 100, formatter: function (value, row, index) {
                return value? value.name:"";
            }
            },
            {field: 'limityear', title: '保障年限', width: 100},
            {field: 'insurance', title: '保额', width: 100},
            {field: 'annualfee', title: '年费', width: 100, align: 'right'},
            {
                field: 'state', title: '状态', width: 100, align: 'right',formatter: function (value, row, index) {

                if (value == 1) {
                    return "<font style='color: green'>在售</font>";
                } else if (value == 0) {
                    return "<font style='color: red'>停售</font>";
                }
            }
            },
            {
                field: 'deduction', title: '不计免赔', width: 100, align: 'right',formatter: function (value, row, index) {

                if (value == 1) {
                    return "<font style='color: green'>是</font>";
                } else if (value == 0) {
                    return "<font style='color: red'>否</font>";
                }
            }
            },

        ]],})
}

$(function(){

    $("#year").numberbox({
        min:0,
        precision:2,
        onChange:function(){
            var year = $("#year").val();
            var rows = $("#in_datagrid").datagrid('getRows');
            var num = 0;
            $.each( rows, function(i, n){
                num = num + n.annualfee;
            });
            num = num * year;
            $("#totalAmount").text(num);
        }
    });

})
function hold(){
    $("#car_Form").form('submit', {
        url:"/storages/saveCar",
        success:function(data){
            $("#info_form").form('submit', {
                url:"/storages/saveSafe",
                onSubmit: function(param){
                    param.limit = $("#totalAmount").text();
                },
                success:function(data){
                    console.log(data);
                }
            });
        }
    });
    $("#car_dialog").dialog("close");
    setTimeout("refresh()","400");


}
$(function(){
    $("#in_datagrid").datagrid({
        fit:true,
        fitColumns:true,
        striped:true,
        pagination:true,
        singleSelect:true,
        rownumbers:true,
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'id', title: '编号', hidden: true, width: 100},
            {field: 'sn', title: '保险编码', width: 100, align: 'right'},
            {field: 'name', title: '产品名称', width: 100},
            {
                field: 'agency', title: '所属机构', width: 100, formatter: function (value, row, index) {
                return value? value.name:"";
            }
            },
            {field: 'limityear', title: '保障年限', width: 100},
            {field: 'insurance', title: '保额', width: 100},
            {field: 'annualfee', title: '年费', width: 100, align: 'right'},
            {
                field: 'state', title: '状态', width: 100, align: 'right',formatter: function (value, row, index) {

                if (value == 1) {
                    return "<font style='color: green'>在售</font>";
                } else if (value == 0) {
                    return "<font style='color: red'>停售</font>";
                }
            }
            },
            {
                field: 'deduction', title: '不计免赔', width: 100, align: 'right',formatter: function (value, row, index) {

                if (value == 1) {
                    return "<font style='color: green'>是</font>";
                } else if (value == 0) {
                    return "<font style='color: red'>否</font>";
                }
            }
            },

        ]],
    })
    var p = $('#in_datagrid').datagrid('getPager');
    $(p).pagination({
        pageSize: 3,//每页显示的记录条数，默认为10
        pageList: [3, 7, 10]//可以设置每页记录条数的列表
    });
})
function query(){
    var row = $("#productInfo_datagrid").datagrid("getSelected");
    if (!row) {
        $.messager.alert('温馨提示', '亲,请先选中你要查的数据');
        return;
    }
    $('#in_datagrid').datagrid('loadData', { total: 0, rows: [] });
    $("#car_dialog").dialog("open");
    $("#car_dialog").dialog("setTitle", "查看投保人明细");
    $("#car_dialog").dialog("setTitle", "编辑保单信息");
    $.get("/productInfo/selectProductForaiId?id="+row.productInfos.id,function(data){
        $("#in_datagrid").datagrid('appendRow',data[0]);
    });
    row["car.displacement"] = row.cars.displacement;
    row["car.carname"] = row.cars.carname;
    row["car.cartype"] = row.cars.cartype;
    row["car.marketval"] = row.cars.marketval;
    row["car.sort"] = row.cars.sort;
    row["car.carsizeId"] = row.cars.carsizeId;
    row["car.id"] = row.cars.id;
    $("#car_Form").form("load",row);
    $('#institution').combobox('select',row.agencyInfos.id);
    row["storages_id"] = row.id;
    row["product_id"] = row.productInfos.id;
    $("#info_form").form("load",row);
    var p = $('#in_datagrid').datagrid('getPager');
    $(p).pagination({
        pageSize: 3,//每页显示的记录条数，默认为10
        pageList: [3, 7, 10]//可以设置每页记录条数的列表
    });
    //数据回显:同名原则
    $("#totalAmount").text(row.limits);
    //角色回显
    $('#car_Form').find('input').attr('readonly',true);
    $('#info_form').find('input').attr('readonly',true);
}
